#include "../../include/buffer.hpp"
#include <iostream>
#include <fstream>

using namespace xzt::log;


int main()
{
    // 从文件中读取日志写入buffer
    std::ifstream ifs("test.log", std::ios::binary);
    if(!ifs.is_open()) { return -1; }
    // 读取文件大小
    ifs.seekg(0, std::ios::end);
    size_t file_size = ifs.tellg();  // 返回当前指针位置距离文件开始位置的字节数
    ifs.seekg(0, std::ios::beg);
    // 先存入字符串中
    std::string content(file_size, '\0');
    ifs.read(&content[0], file_size);
    // 关闭文件
    ifs.close();


    // 读取文件内容写入buffer
    Buffer buffer;  // 缓冲区大小为默认值, 故意测试扩容
    buffer.push(&content[0], file_size);
    // 测试读到的字节数
    std::cout << "read size: " << buffer.readAbleSize() << std::endl;

    // 重新写入新文件
    std::ofstream ofs1("tmp_1.log", std::ios::binary);
    ofs1.write(buffer.begin(), buffer.readAbleSize());
    ofs1.close();

    std::ofstream ofs2("tmp_2.log", std::ios::binary);
    size_t rsize = buffer.readAbleSize();
    for(size_t i = 0; i < rsize; ++i)
    {
        ofs2.write(buffer.begin(), 1);
        buffer.moveReadPtr(1);
    }
    ofs2.close();


    
    return 0;
}

